
define('changePersonData',['common','config'],function(utils,config){
	'use strict';
	var changePersonData = (function(data,suc){
		var url = utils.getWebUrl('user/update');
		var data= data ;
		utils.ajax(url,data,'POST',function(res){
			suc(res);
		},function(){
			$('.DataLoading').show();
		},function(){
			$('.DataLoading').hide();
		})
	})
	return changePersonData;
});

define([
		'vue',
		'config',
		'common',
		'moment',
		'changePersonData'
		],function(Vue,config,utils,moment,changePersonData){
	'use strict';

	var personalDataView=new Vue({
		el:'#app',
		data:{
			avatarUrl:'',//../image/Avatar.png
			userName:'',
			brithday:'',
			sex:'',
			sphone:'',
			phone:'',
			phoneCode:'',
			lastPhoneCode:'',
			newPhone:'',
			userType:'',
			shopsStatus:'',
			backtype:'',

			secondOfDelay : 60000 ,
			secondLeftOfDelay : 0 
		},
		filters:{
			decodeURI:function(value){
				return value;
			},
			moment: function (date) {
			    return moment(date).format('YYYY-MM-DD');
			}
		},
		created:function(){
			utils.userToken();

			var _this=this;
			_this.backtype =utils.getQueryVariable('type');
			utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
				//判断商家B或会员M
				_this.userType = res.data.userCode.slice(0,1)
				//判断审核商家是否通过
				_this.shopsStatus = res.data.shopsStatus;

				_this.userName = res.data.userName? res.data.userName: null;
				_this.brithday = res.data.brithday? utils.localDate(res.data.brithday): null;
				var sex = res.data.sex;
				if(sex === '0'){
					_this.sex = '女';
				}else if(sex === '1'){
					_this.sex = '男';
				}

				$("#selSex option[value="+sex+"]").attr("selected", "selected");

				var logoImg = res.data.logoImg;
				if(!logoImg){
					_this.avatarUrl = '../image/Avatar.png';
				}else{
					_this.avatarUrl = '../image/'+logoImg+'@2x.png';
				}
				
				_this.sphone = utils.showPhone(res.data.phone);
				_this.phone = res.data.phone;

			},function(){
				$('.DataLoading').show();
			},function(){
				$('.DataLoading').hide();
			})	
		},
		methods:{
			back:function(){
				if(this.backtype == 1){
	       			if(this.userType == 'M'){
						window.location.href='userInfo.html';
					}else{
						window.location.href='shopInfo.html';
					}
	       		}else{
	       			window.location.href='Settings.html';
	       		}
			},
			toAvatar:function(){
				window.location.href='avatarList.html?type='+this.backtype;
			},
			/*修改用户名*/
			save:function(){
				if($.trim(this.userName) === ""){
                    utils.appAlert('用户名不能为空');
                    return;
				}
				var data =	{
					userName:this.userName
				}
				changePersonData(data,function(res){
					window.location.href="personalData.html";
				});
			},
			/*添加选择生日picker*/
			addPickerBirth:function(){
				var myApp = new Framework7();
				var today = new Date();
				var _this = this;
				utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
					if(res.data.brithday !==null){
						var selbrithday = moment(res.data.brithday*1000).format('YYYY-MM-DD');
						var selbrithday = selbrithday.split('-');
						_this.selYear = selbrithday[0]*1;
						_this.selMonth = selbrithday[1]*1-1;
						_this.selDate = selbrithday[2]*1;
					}else{
						_this.selYear = today.getFullYear();
						_this.selMonth = today.getMonth();
						_this.selDate = today.getDate();
					}
					
					_this.pickerBirthDay = myApp.picker({
					    input: '#pickerBirthday',
					    rotateEffect: true,
					    toolbarCloseText:'确定',
					
					    value: [_this.selYear,_this.selMonth,_this.selDate],
					 
					    onChange: function (picker, values, displayValues) {
					    	//如果大于当前年月
					    	var Month = today.getMonth();
					    	var Year = today.getFullYear();
					    	var day = today.getDate();
					    	if(values[0] == Year){

					    		if (values[1] > Month) {
						            picker.cols[1].setValue(Month);
						        }
						        if (values[2] > day && values[1] == Month){
						        	picker.cols[2].setValue(day);
						        }
					    	}
					    	//如果大于月份日期
					        var daysInMonth = new Date(picker.value[0], picker.value[1]*1+1, 0).getDate();
					        if (values[2] > daysInMonth) {
					            picker.cols[2].setValue(daysInMonth);
					        }
					    },
					 
					    formatValue: function (p, values, displayValues) {
					        return displayValues[0] + ' ' + values[1] + ', ' + values[2];
					    },

					    onOpen:function(picker){
							$('.dialog').show();
					    },

					    onClose:function(picker){
					    	$('.dialog').hide();
					    	$('.picker-modal').hide();
					    	var yy = picker.value[0];
					    	var mm = picker.value[1]*1+1;
					    	var dd = picker.value[2];
					    	var data = {
								brithday: yy +'-'+ mm +'-'+ dd
					    	}	
					    	changePersonData(data,function(res){
					    		_this.brithday = yy+'年'+mm+'月'+dd+'日';
					    	})
	 				    },
					 
					    cols: [
					    	// Years
					        {
					            values: (function () {
					                var arr = [];
					                for (var i = 1950; i <= today.getFullYear(); i++) { arr.push(i); }
					                return arr;
					            })(),
					            displayValues: (function () {
					                var arr = [];
					                for (var i = 1950; i <= today.getFullYear(); i++) { arr.push(i+"年"); }
					                return arr;
					            })(),
					        },
					        // Months
					        {
					            values: ('0 1 2 3 4 5 6 7 8 9 10 11').split(' '),
					            displayValues: ('1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月').split(' '),
					            textAlign: 'center'
					        },
					        // Days
					        {
					            values: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],
					            displayValues: (function () {
					                var arr = [];
					                for (var i = 1; i <= 31; i++) { arr.push(i+"日"); }
					                return arr;
					            })()
					        }
					    ]
					});
					_this.pickerBirthDay.open();
				})

			},
			/*添加选择性别picker*/
			addPickersex:function(){
				var _this = this;
				var myApp = new Framework7();
				utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
					var selSex;
					if(res.data.sex == 0){
						selSex = '女';
					}else{
						selSex = '男';
					}

					_this.pickerSex = myApp.picker({
					    input: '#pickersex',
					    toolbarCloseText:'确定',
					    value: [selSex],
					    cols: [
					        {
					            textAlign: 'center',
					            values: ['男', '女']
					        }
					    ],
					    onOpen:function(picker){
							$('.dialog').show();
					    },
					    onClose:function(picker){
					    	// console.log(picker.cols[0].value)
					    	$('.dialog').hide();
					    	var sexvalue = picker.cols[0].value;
							var sexDisplay,avatarSex;
							if(sexvalue.indexOf('男') === -1){
								sexDisplay = 0;
								avatarSex = 'girl';
							}else{
								sexDisplay = 1;
								avatarSex = 'boy';
							}
							//改变头像
							function RandomNumBoth(Min,Max){
								var Range = Max - Min;
								var Rand = Math.random();
								var num = Min + Math.round(Rand * Range); //四舍五入
								return num;
							}

							var picPath = avatarSex+'0'+RandomNumBoth(1,5);
							
					    	var data = {
								sex: sexDisplay,
								picPath: picPath,
					    	}

					    	changePersonData(data,function(res){
					    		_this.sex = sexvalue;
								_this.avatarUrl = '../image/'+ picPath +'@2x.png';

					    	})
					    }
					});
					_this.pickerSex.open();
				})
			},
			selectSex:function(event){
				var _this = this;
				var selValue = $(event.currentTarget).val();
				//console.log(typeof(selValue))
				var avatarSex;

				utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
					if(selValue === '0'){
						avatarSex = 'girl';
					}else{
						avatarSex = 'boy';
					}
					//改变头像
					function RandomNumBoth(Min,Max){
						var Range = Max - Min;
						var Rand = Math.random();
						var num = Min + Math.round(Rand * Range); //四舍五入
						return num;
					}

					var picPath = avatarSex+'0'+RandomNumBoth(1,5);
			    	changePersonData({
			    		sex: selValue,
						picPath: picPath,
			    	},function(res){
			    		if(selValue === '0'){
							_this.sex = '女';
						}else{
							_this.sex = '男';
						}
			    		
						_this.avatarUrl = '../image/'+ picPath +'@2x.png';
			    	})
				})
				
			}	
		}
	})

	return personalDataView
});